<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import {getList,getDetail,postSave,getDict} from '@/api/omSale/vehicleParameters/fieldType'
import { dictionary } from '@/utils/dictMixin';
export default {
  data(){
    this.saveData=null;
    return{
      formList:this.createdFrom(),
      list:[],
      selectData:{}
    }
  },
  created(){
    this.getDict()
  },
  mixins:[dictionary],
  methods:{
    createdFrom(){
      return [
        {
          type: 'SEARCH_HELPER',
          label: '字段类型',
          fieldName: 'name',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          request:{
            fetchApi:getDict,
            params:{},
            valueKey:'type',
            textKey:'typeName',
            change:p=>{
              console.log(p,'改变')
              this.selectData=p;
            }
          },
        },
        {
          type: 'SELECT',
          label: '启用标记',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          initialValue:10031001,
          rules: [{ required: true, message: '请选择', trigger: 'change' }],

        },

        {
          type: 'INPUT',
          label: '名称',
          fieldName: 'codeCnDesc',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
      ]
    },
    changeHandle(val){
      this.saveData=val;
    },
    async getDict(){
      let res=await getDict();
      this.list=res.data;
    },
    async closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        this.$refs.from.SUBMIT_FORM();
        console.log(this.saveData, 'this.saveData');
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.type=this.selectData.text;
          json.typeName=this.selectData.value;
          if(!json.type||json.name!=json.typeName){
            this.$notify({
              title: '警告',
              message: '请选择有效的字符类型',
              type: 'warning'
            });
            return;
          }

          let res = await postSave(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          }else{
            this.saveData=null;
          }
        }

      }
    }
  }
}
</script>
